﻿using Abp.Application.Services;
using Abp.Runtime.Validation;
using Abp.UI;
using Camc.Quality.FileCommon;
using Camc.Quality.FileCommon.Dto;
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net;

namespace Camc.Quality.Quality
{
    public class FileCommonService : ApplicationService, IFileCommonService
    {
        /// <summary>
        /// 上传一个文件，并返回文件上传成功后的信息
        /// </summary>
        /// <param name="File">要上传的文件实体</param>
        /// <returns>文件上传成功后返回的文件相关信息</returns>
        public async Task<FileUploadOutputDto> UploadFile(IFormFile File)
        {

            try
            {
                //文件的原始名称
                string FileOriginName = File.FileName;

                //读取文件保存的根目录
                string fileSaveRootDir = ConfigHelper.GetAppSetting("App", "FileRootPath");
                //读取办公管理文件保存的模块的根目录
                string fileSaveDir = ConfigHelper.GetAppSetting("App", "OAFiles");
                //文件保存的相对文件夹(保存到wwwroot目录下)
                string absoluteFileDir = fileSaveRootDir + "/" + fileSaveDir;

                //文件保存的路径(应用的工作目录+文件夹相对路径);
                string fileSavePath = Environment.CurrentDirectory + "/wwwroot/" + absoluteFileDir;
                if (!Directory.Exists(fileSavePath))
                {
                    Directory.CreateDirectory(fileSavePath);
                }

                //生成文件的名称
                string Extension = Path.GetExtension(FileOriginName);//获取文件的源后缀
                if (string.IsNullOrEmpty(Extension))
                {
                    throw new UserFriendlyException("文件上传的原始名称好像不对哦，没有找到文件后缀");
                }
                string fileName = Guid.NewGuid().ToString() + Extension;//通过uuid和原始后缀生成新的文件名

                //最终生成的文件的相对路径（xxx/xxx/xx.xx）
                string finalyFilePath = fileSavePath + "/" + fileName;

                FileUploadOutputDto result = new FileUploadOutputDto();


                //打开上传文件的输入流
                Stream stream = File.OpenReadStream();
                //创建输入流的reader
                //var fileType = stream.GetFileType();
                //文件大小
                result.FileLength = stream.Length;
                result.FileName = FileOriginName;
                result.FileType = Extension.Substring(1);
                result.FileUrl = absoluteFileDir + "/" + fileName;

                //开始保存拷贝文件
                FileStream targetFileStream = new FileStream(finalyFilePath, FileMode.OpenOrCreate);
                await stream.CopyToAsync(targetFileStream);


                return result;
            }
            catch (Exception ex)
            {
                throw new UserFriendlyException("文件上传失败，原因" + ex.Message);
            }
        }


        public ImgInfo DownLoadIMG(string classTable, string code)
        {

            /// <summary>
            /// 下载图片
            /// </summary>
            /// <param name="webClient"></param>
            /// <param name="url">图片url</param>
            /// <param name="folderPath">文件夹路径</param>
            /// <param name="fileName">图片名</param>

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(classTable);
            WebClient wc = new WebClient();
            //读取文件保存的根目录
            string fileSaveRootDir = ConfigHelper.GetAppSetting("App", "IMGRootPath");


            //文件保存的路径(应用的工作目录+文件夹相对路径);
            string fileSavePath = Environment.CurrentDirectory + "/wwwroot/" + fileSaveRootDir + "/" + code;
            if (!Directory.Exists(fileSavePath))
            {
                Directory.CreateDirectory(fileSavePath);
            }

            HtmlNode node = doc.DocumentNode.SelectSingleNode("//a/img");
            var url = node.Attributes["src"].Value;
            if (url.IndexOf("https:") == -1 && url.IndexOf("http:") == -1)
            {
                url = "http://n.sinaimg.cn/" + url;
            }
            var imgName = Guid.NewGuid().ToString("D");
            //下载图片
            try
            {
                wc.DownloadFile(url, fileSavePath + "/" + imgName + ".jpg");
                Console.WriteLine(imgName + "下载成功");
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                Console.WriteLine(url);
            }
            return new ImgInfo()
            {
                Url = fileSavePath + "/" + imgName,
                Name = imgName
            };
        }

    }
    public class ImgInfo
    {
        public string Url { get; set; }
        public string Name { get; set; }
    }
}

